package com.evaluationsystem.system.serivce.impl;

import com.evaluationsystem.system.dao.AssessStatisticalDao;
import com.evaluationsystem.system.serivce.AssessStatisticalService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Service;
import org.springframework.util.ResourceUtils;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class AssessStatisticalServiceImpl implements AssessStatisticalService {

    @Autowired
    AssessStatisticalDao dao;

    @Override
    public Map<String, Object> StatisticalTable(Map<String, Object> param) {
        Map<String,Object> map = new HashMap<>();
        PageHelper.startPage(Integer.parseInt(param.get("page").toString()), Integer.parseInt(param.get("limit").toString()));
        List<Map<String, Object>> staffInfos =  dao.StatisticalTable(param);
        PageInfo<Map<String, Object>> PageInfos = new PageInfo<>(staffInfos);
        map.put("code","0");//解析接口状态
        map.put("msg","success");//解析提示文本
        map.put("count",PageInfos.getTotal());//解析数据长度
        map.put("data",PageInfos.getList());//解析数据列表
        return map;
    }

    @Override
    public Workbook exportExcel(Map<String, Object> param) throws IOException {

        List<Map<String, Object>> staffInfos =  dao.StatisticalTableExcel(param);
        System.out.println(param);
        //加载 Excel 模板
        /*Resource resource =  new ClassPathResource(ResourceUtils.CLASSPATH_URL_PREFIX+"/templates/excelTemplate/AssessStatistical.xlsx");
        FileInputStream fis = new FileInputStream(resource.getFile());*/
        InputStream resourceAsStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("excelTemplate/AssessStatistical.xlsx");
        //根据模板 创建工作薄
        Workbook wb = new XSSFWorkbook(resourceAsStream);
        //读取工作表
        Sheet sheet = wb.getSheetAt(0);
        //抽取公共样式
        Row row0 = sheet.getRow(0);
        row0.getCell(0).setCellValue("员工绩效考核成绩汇总（"+param.get("start")+"）");
        Row row = sheet.getRow(3);
        CellStyle[] style = new CellStyle[row.getLastCellNum()];
        for (int i = 0; i < style.length ; i++) {
            Cell cell = row.getCell(i);
            style[i] = cell.getCellStyle();
        }
        //构造并编辑单元格
        Cell cell = null;
        int createRownum=0,befor=0;
        for (int i = 0; i < staffInfos.size() ; i++) {
            List<Map<String, Object>> childrens = (List<Map<String, Object>> )staffInfos.get(i).get("children");
            createRownum = i==0?createRownum + i + 3:createRownum + 1;
            if (childrens.size()==1){
                row =  sheet.createRow(createRownum);
                cell = row.createCell(0);
                cell.setCellValue(staffInfos.get(0).get("vfactoryflgname")+""+staffInfos.get(i).get("vteamname"));
                cell.setCellStyle(style[0]);

                cell = row.createCell(1);
                cell.setCellValue(childrens.get(0).get("vstaffname").toString());
                cell.setCellStyle(style[1]);

                cell = row.createCell(2);
                cell.setCellValue(childrens.get(0).get("grade").toString());
                cell.setCellStyle(style[2]);

                cell = row.createCell(3);
                cell.setCellValue(childrens.get(0).get("zwfpercentagescore").toString());
                cell.setCellStyle(style[3]);

                cell = row.createCell(4);
                cell.setCellValue(childrens.get(0).get("bzfpercentagescore").toString());
                cell.setCellStyle(style[4]);

                cell = row.createCell(5);
                cell.setCellValue(childrens.get(0).get("kufpercentagescore").toString());
                cell.setCellStyle(style[5]);

                cell = row.createCell(6);
                cell.setCellValue(childrens.get(0).get("pdfpercentagescore").toString());
                cell.setCellStyle(style[6]);

                cell = row.createCell(7);
                cell.setCellValue(childrens.get(0).get("grossscore").toString());
                cell.setCellStyle(style[7]);
            }else {
                befor = createRownum;
                row =  sheet.createRow(createRownum);
                cell = row.createCell(0);
                cell.setCellValue(staffInfos.get(i).get("vfactoryflgname")+"\r\n"+staffInfos.get(i).get("vteamname"));
                cell.setCellStyle(style[0]);
                for (int j = 0; j < childrens.size() ; j++){
                    if (j > 0){
                        createRownum = createRownum + 1;
                        row =  sheet.createRow(createRownum);
                    }
                    cell = row.createCell(1);
                    cell.setCellValue(childrens.get(j).get("vstaffname").toString());
                    cell.setCellStyle(style[1]);

                    cell = row.createCell(2);
                    cell.setCellValue(childrens.get(j).get("grade").toString());
                    cell.setCellStyle(style[2]);

                    cell = row.createCell(3);
                    cell.setCellValue(childrens.get(j).get("zwfpercentagescore").toString());
                    cell.setCellStyle(style[3]);

                    cell = row.createCell(4);
                    cell.setCellValue(childrens.get(j).get("bzfpercentagescore").toString());
                    cell.setCellStyle(style[4]);

                    cell = row.createCell(5);
                    cell.setCellValue(childrens.get(j).get("kufpercentagescore").toString());
                    cell.setCellStyle(style[5]);

                    cell = row.createCell(6);
                    cell.setCellValue(childrens.get(j).get("pdfpercentagescore").toString());
                    cell.setCellStyle(style[6]);

                    cell = row.createCell(7);
                    cell.setCellValue(childrens.get(j).get("grossscore").toString());
                    cell.setCellStyle(style[7]);
                }
                sheet.addMergedRegion(new CellRangeAddress(befor,createRownum,0,0));
            }
        }
        return wb;

    }
}
